#![allow(unused_imports)]
use eztrade_dto::{
    DataTransferObject,
    Message,
    tick::{Orderstack, TickData},
    order::{Order},
};
use bytes::Bytes;
use rstest::*;
use chrono::prelude::*;


#[rstest]
fn test_notification_serialization() {
    let notification = Message::Notification("Test notification".to_string());

    // 测试 JSON 序列化
    let json_bytes = notification.to_json_bytes().expect("JSON 序列化失败");
    println!("JSON 输出: {}", std::str::from_utf8(&json_bytes).expect("字节转字符串失败"));
    assert!(!json_bytes.is_empty(), "JSON 字节数组不应为空");

    // 测试 JSON 反序列化
    let deserialized = Message::from_json_bytes(&json_bytes).expect("JSON 反序列化失败");
    assert_eq!(notification, deserialized, "序列化和反序列化的数据应相等");

    // 测试二进制序列化
    let binary_bytes = notification.to_binary_bytes().expect("二进制序列化失败");
    println!("二进制输出: {:?}", binary_bytes);
    assert!(!binary_bytes.is_empty(), "二进制字节数组不应为空");

    // 测试二进制反序列化
    let deserialized = Message::from_binary_bytes(&binary_bytes).expect("二进制反序列化失败");
    assert_eq!(notification, deserialized, "序列化和反序列化的数据应相等");
}

#[fixture]
fn sample_message() -> Message {
    Message::TickData(
        TickData::new(
            1627372800000000, // 2021-07-27 08:00:00 UTC
            "BTCUSDT".to_string(),
            45000.50,
            vec![
                Orderstack { price: 45001.0, volume: 100 },
                Orderstack { price: 45002.0, volume: 200 },
                Orderstack { price: 45003.0, volume: 150 },
                Orderstack { price: 45003.5, volume: 904 },
                Orderstack { price: 45003.6, volume: 920 },
            ],
            vec![
                Orderstack { price: 45000.0, volume: 80 },
                Orderstack { price: 44999.0, volume: 120 },
                Orderstack { price: 44998.0, volume: 902 },
                Orderstack { price: 44997.0, volume: 90 },
                Orderstack { price: 44996.0, volume: 390 },
            ]
        ))
}

#[rstest]
fn test_json_serialization_deserialization(sample_message: Message) {
    // 测试 JSON 序列化
    let json_bytes = sample_message.to_json_bytes().expect("序列化失败");
    assert!(!json_bytes.is_empty(), "JSON 字节数组不应为空");

    // 验证 JSON 格式
    let json_str = std::str::from_utf8(&json_bytes).expect("字节转字符串失败");
    println!("JSON 输出: {}", json_str);
    assert!(json_str.contains("BTCUSDT"), "JSON 应包含交易对信息");
    assert!(json_str.contains("45000.5"), "JSON 应包含最后成交价");
    println!("JSON 数据长度: {} 字节", json_bytes.len());

    // 测试 JSON 反序列化
    let deserialized = Message::from_json_bytes(&json_bytes).expect("反序列化失败");
    assert_eq!(sample_message, deserialized, "序列化和反序列化的数据应相等");
}

#[rstest]
fn test_binary_serialization_deserialization(sample_message: Message) {
    // 测试二进制序列化
    let binary_bytes = sample_message.to_binary_bytes().expect("二进制序列化失败");
    assert!(!binary_bytes.is_empty(), "二进制字节数组不应为空");
    println!("二进制输出: {:?}", binary_bytes);
    println!("二进制数据长度: {} 字节", binary_bytes.len());

    // 测试二进制反序列化
    let deserialized = Message::from_binary_bytes(&binary_bytes).expect("二进制反序列化失败");
    assert_eq!(sample_message, deserialized, "二进制序列化和反序列化的数据应相等");
}

#[rstest]
fn test_json_vs_binary_size(sample_message: Message) {
    // 比较 JSON 和二进制序列化的大小
    let json_bytes = sample_message.to_json_bytes().expect("JSON 序列化失败");
    let binary_bytes = sample_message.to_binary_bytes().expect("二进制序列化失败");

    println!("JSON 大小: {} 字节", json_bytes.len());
    println!("二进制大小: {} 字节", binary_bytes.len());

    // 通常二进制序列化会更紧凑
    assert!(binary_bytes.len() > 0, "二进制数据应有内容");
    assert!(json_bytes.len() > 0, "JSON 数据应有内容");
}

#[rstest]
#[case("ETHUSDT", 3500.25, 10)]
#[case("ADAUSDT", 1.45, 5)]
#[case("DOGEUSDT", 0.25, 3)]
fn test_different_symbols(#[case] symbol: &str, #[case] price: f32, #[case] volume: u32) {
    let tick_data = TickData::new(
        1627372800000,
        symbol.to_string(),
        price,
        vec![Orderstack { price: price + 0.01, volume }],
        vec![Orderstack { price: price - 0.01, volume }],
    );

    // 测试 JSON 序列化往返
    let json_bytes = tick_data.to_json_bytes().expect("JSON 序列化失败");
    let json_deserialized = TickData::from_json_bytes(&json_bytes).expect("JSON 反序列化失败");
    assert_eq!(tick_data, json_deserialized);

    // 测试二进制序列化往返
    let binary_bytes = tick_data.to_binary_bytes().expect("二进制序列化失败");
    let binary_deserialized = TickData::from_binary_bytes(&binary_bytes).expect("二进制反序列化失败");
    assert_eq!(tick_data, binary_deserialized);
}

#[rstest]
fn test_empty_orderbook_lists() {
    let tick_data = TickData::new(
        1627372800000,
        "TESTSYMBOL".to_string(),
        100.0,
        vec![], // 空的卖单列表
        vec![], // 空的买单列表
    );

    // JSON 序列化测试
    let json_bytes = tick_data.to_json_bytes().expect("JSON 序列化失败");
    let json_deserialized = TickData::from_json_bytes(&json_bytes).expect("JSON 反序列化失败");
    assert_eq!(tick_data, json_deserialized);
    assert!(json_deserialized.asks.is_empty());
    assert!(json_deserialized.bids.is_empty());

    // 二进制序列化测试
    let binary_bytes = tick_data.to_binary_bytes().expect("二进制序列化失败");
    let binary_deserialized = TickData::from_binary_bytes(&binary_bytes).expect("二进制反序列化失败");
    assert_eq!(tick_data, binary_deserialized);
    assert!(binary_deserialized.asks.is_empty());
    assert!(binary_deserialized.bids.is_empty());
}

#[rstest]
fn test_invalid_json_deserialization() {
    let invalid_json = Bytes::from("这不是有效的 JSON");
    let result = TickData::from_json_bytes(&invalid_json);
    assert!(result.is_err(), "无效 JSON 应该反序列化失败");
}

#[rstest]
fn test_invalid_binary_deserialization() {
    let invalid_binary = Bytes::from(vec![1, 2, 3, 4, 5]); // 随机字节
    let result = TickData::from_binary_bytes(&invalid_binary);
    assert!(result.is_err(), "无效二进制数据应该反序列化失败");
}

fn main() {
    println!("运行 TickData 序列化测试...");
    println!("使用 'cargo test --example test_dto' 来运行这些测试");
}
